package pl.com.sthaether.mc.engine.data;

import com.vividsolutions.jts.geom.MultiPolygon;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.scene.chart.Chart;
import javafx.scene.chart.PieChart;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.text.Text;
import javafx.scene.transform.Scale;
import javafx.scene.transform.Translate;
import pl.com.sthaether.mc.config.MCCurrentState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MCMapDataObject {

    private Text text;
    private StringProperty name;
    private Chart chart;
    private List<Path> pathList;
    private MultiPolygon shape;
    private DoubleProperty objectValue;

    public Map<String, DoubleProperty> getChartData() {
        return chartData;
    }

    private Map<String, DoubleProperty> chartData;

    public MCMapDataObject(String name, MultiPolygon shape) {
        this.name = new SimpleStringProperty(name);
        this.text = new Text(name);
        this.shape = shape;
        this.pathList = new ArrayList<>();
        this.chartData = new HashMap<>();
        this.objectValue = new SimpleDoubleProperty();
        this.chart = new PieChart();
        this.chart.setLegendVisible(false);
        ((PieChart) this.chart).setLabelsVisible(false);
//        this.chart.titleProperty().bind(text.textProperty());


        Bounds bounds = this.text.getBoundsInLocal();
        this.text.getTransforms().add(new Translate(shape.getCentroid().getX(), shape.getCentroid().getY()));
        this.text.getTransforms().add(new Scale(0.012, -0.012));
        this.text.getTransforms().add(new Translate(-bounds.getWidth() / 2., bounds.getHeight() / 2.));

        this.text.textProperty().bindBidirectional(this.name);
    }


    public void setObjectColor(Color color) {
        for (Path path : pathList) {
            path.setFill(color);
        }
    }

    public double getObjectValue() {
        return objectValue.getValue();
    }

    public DoubleProperty objectValueProperty() {
        return objectValue;
    }


    public List<Path> getPathList() {
        return pathList;
    }

    public void addPath(Path p) {
        pathList.add(p);
    }


    public String getName() {
        return name.getValue();
    }

    public void setName(String name) {
        this.name.setValue(name);
    }

    public Text getText() {
        return text;
    }


    public void setObjectVisible(boolean visible) {
        if (chart != null) {
            chart.setVisible(visible);
        }
        text.setVisible(visible);
        for (Path path : pathList) {
            path.setVisible(visible);
        }
    }

    public MultiPolygon getShape() {
        return shape;
    }

    public void updateChart() {
        MCCurrentState.getInstance().getCurrentMap().getData().getDataGroup().getChildren().remove(this.chart);
        this.chart = new PieChart();
        this.chart.setPrefWidth(300);
        this.chart.setPrefHeight(300);
        this.chart.setLegendVisible(false);
        ((PieChart) this.chart).setLabelsVisible(false);
        this.chart.setStyle("  -fx-tick-label-font-size: 10pt  ");

        MCCurrentState.getInstance().getCurrentMap().getData().getDataGroup().getChildren().add(this.chart);


        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        List<MCSeriesColor> cartodiagramSeries = MCCurrentState.getInstance().getCurrentMap().getData().getCartodiagramSeries();
        for (Map.Entry<String, DoubleProperty> o : chartData.entrySet()) {
            pieChartData.add(new PieChart.Data(o.getKey(), o.getValue().getValue()));
        }

        boolean shouldNotBeVisible = true;
        ((PieChart) this.chart).setData(pieChartData);
        for (PieChart.Data data : ((PieChart) this.chart).getData()) {
            for (MCSeriesColor c : cartodiagramSeries) {
                if (c.getName().equals(data.getName())) {
                    data.getNode().setStyle("-fx-pie-color: " + c.getColor().toString().replace("0x", "#") + ";");
                }
            }
            if (data.getPieValue() > 0) {
                shouldNotBeVisible = false;
            }
        }
        if (chartData.values().size() <= 0 || shouldNotBeVisible) {
            this.chart.setVisible(false);
        } else {
            this.chart.setVisible(true);

            this.chart.getTransforms().add(new Translate(shape.getCentroid().getX(), shape.getCentroid().getY()));
            this.chart.getTransforms().add(new Scale(0.005, -0.005));
            this.chart.getTransforms().add(new Translate(-this.chart.getPrefWidth() / 2., -this.chart.getPrefHeight() / 2.));
        }
    }

}
